#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

// 使用 std::recursive_mutex : 递归互斥锁允许同一个线程多次锁定同一个互斥锁
// mutex m1:  mutex类型锁不支持单次线程多次上锁
recursive_mutex rmtx;
int shared_resource = 0;
void recursive_increment(int count)
{
    if (count <= 0)
        return;
    rmtx.lock();
    ++shared_resource;
    recursive_increment(count - 1);
    rmtx.unlock();
}

int main()
{
    thread t1(recursive_increment, 3);
    thread t2(recursive_increment, 3);
    t1.join();
    t2.join();
    cout << "Final value of shared_resource: " << shared_resource << endl;
    return 0;
}